package com.qd.tfpay.order;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

@Data
@Accessors(chain = true)
@ToString
@Slf4j
public class BaseResponseBO {
    /**
     * 原始报文
     */
    private JsonNode originalMessage;

    private static final String RESPONSE_SUCCESS = "0000";
    /**
     * 接口名称
     */
    private String service;
    /**
     * 接口版本
     */
    @JsonProperty("service_version")
    private String serviceVersion;
    /**
     * 字符编码，固定值：UTF-8
     */
    @JsonProperty("input_charset")
    private String inputCharset;
    /**
     * 签名类型，固定值：MD5
     */
    @JsonProperty("sign_type")
    private String signType;
    /**
     * 签名，用于判断商户真实性，防止报文被篡改
     */
    @JsonProperty("sign")
    private String sign;
    /**
     * 0000 表示成功，其他表示失败
     */
    private String retCode;
    /**
     * 响应消息
     */
    private String retMsg;
    /**
     * 响应的序列号，用于标示不同的请求
     */
    private String returnSerNo;


    public BaseResponseBO() {
    }

    public boolean isSuccess() {
        return null != retCode && retCode.equals(RESPONSE_SUCCESS);
    }

    public void freeData() {
        this.originalMessage = null;
        this.service = null;
        this.serviceVersion = null;
        this.inputCharset = null;
        this.signType = null;
        this.sign = null;
        this.retCode = null;
        this.retMsg = null;
        this.returnSerNo = null;
    }

//    @SneakyThrows
//    public static void main(String[] args) {
//        String s1 = "{\"retCode\":\"0000\",\"retMsg\":\"成功\",\"retMsgDetail\":\"成功\",\"returnSerNo\":\"tpcg0701101320tdsvjh\",\"partner\":\"1202851715\",\"subpartner\":\"1202851718\",\"out_trade_no\":\"14888878661960007629640441204473\",\"transaction_id\":\"20220701101739000136338619\",\"trade_state\":\"3\",\"trans_channel\":\"wx_lite\",\"total_fee\":\"352.50\",\"fee_type\":\"1\",\"time_end\":\"20220701 10:17:40\",\"attach\":\"1542693888650829826\",\"returnMsg\":\"{\\\"qr_code\\\":\\\"appId=wx6ff8cd570753a2af&nonceStr=fd10f7066ffa4b53a92ad9a2348b11a0&package=prepay_id=wx011017402881760520d383a49a014c0000&paySign=p+zVNKZ7TUJ/MX+qfr/uH8t1qes1YS0l8UEKu0RSWWjTs+FFVm3G+FEp1M+5fa3RQdpjXW77/owdlAUoQ3b3vDeKPpM1vbYiRbF9T9VYf+K4v524t6f97/aae9Jav+ZAdtIam1WTd9kFjj/rQ3G1nDnD0XmDGPuBLT/UM26+T8Hq4oufVQGgmQEZbc69H6qZqp4PD9N8wa+Ri5yh84HryUtGPDVsjP1Vpe5ZvwhRqeZcxSEi6rRxXslYg6EIXBe0Re6tX0Rqf44Qhptbz0vO6fnGSwFFC/au9S9dAZ8jUcpERTUI/QhamEF56ayAgFoFXPjkvjhbwfdUYr7of6cTUA==&signType=RSA&timeStamp=1656641860\\\"}\",\"pay_transno\":\"20220701000332183\",\"service\":\"pay_service\",\"service_version\":\"1.1\",\"input_charset\":\"UTF-8\",\"sign_type\":\"MD5\",\"sign\":\"03b1745ffaa21f75406fd8d9c0aee807\"}";
//        final JsonNode jsonNode = new ObjectMapper().readTree(s1);
//        String returnMsg = jsonNode.get("returnMsg").asText();
//        returnMsg = returnMsg.substring(returnMsg.indexOf("appId"), returnMsg.lastIndexOf("\"}"));
//        final String[] returnMsgList = returnMsg.split("&");
//        Map<String, String> returnParams = new HashMap<>(16);
//        for (String s : returnMsgList) {
//             String key = s.substring(0,s.indexOf("="));
//             String value = s.substring(key.length()+1);
//            System.out.println("key: "+key+",value: "+value);
//        }
//    }
}
